package com.mengfou.task;

import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskCollection;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 梦否
 * @date 2022/12/07 22:27
 */
public class TaskHelper {

    public static List<Task> findTaskByName(Project project, Class<? extends Task> clazz, String variantName) {
        TaskCollection<? extends Task> targetTasks = project.getTasks().withType(clazz).matching(task -> {
            return task.getName().toLowerCase().contains(variantName);
        });
        return new ArrayList<>(targetTasks);
    }

    public static Task findTaskByName(Project project, String taskName, String variantName) {
        if(!taskName.contains(variantName)) {
            taskName = taskName.replace(variantName.equalsIgnoreCase("debug") ? "release" : "debug", variantName);
        }
        return project.getTasks().getByName(taskName);
    }
}
